package com.act.moudles.user.controller;

import com.act.moudles.user.entity.Menu;
import com.act.moudles.user.entity.ComBo;
import com.act.moudles.user.pojo.ComboPojo;
import com.act.moudles.user.service.MenuService;
import com.act.moudles.user.service.ComboService;
import com.act.utils.DataResult;
import com.act.utils.PageSearch;
import com.act.utils.vueMenu.TreeSelect;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = "通用菜单套餐")
@RequestMapping("user/combo")
@Slf4j
public class ComboController {

    @Resource
    ComboService refMenuTempService;

    @Resource
    private MenuService menuService;

    @PostMapping("list")
    @ApiOperation("查询套餐列表")
    public DataResult listAllCombos(){
        return DataResult.success("查询成功！",refMenuTempService.list());
    }


    @PostMapping("page")
    @ApiOperation("套餐分页查询")
    public DataResult page(@RequestBody PageSearch<ComBo> pageSearch) {
        Page<ComBo> page = Page.of(pageSearch.getPageNum(),pageSearch.getPageSize());
        return DataResult.success("查询成功！",refMenuTempService.pageTemp(page,pageSearch.getDomain()));
    }

    @PostMapping("add")
    @ApiOperation("新增套餐")
    public DataResult add(@RequestBody ComboPojo refMenuTempPojo){
        //新增
        String addRes = refMenuTempService.addTemp(refMenuTempPojo);
        if(StringUtils.isNotEmpty(addRes)){
            return DataResult.fail(addRes);
        }
        return DataResult.success("新增成功！");
    }

    @PostMapping("modify")
    @ApiOperation("修改套餐")
    public DataResult modify(@RequestBody ComboPojo refMenuTempPojo){
        //新增
        String addRes = refMenuTempService.modifyTemp(refMenuTempPojo);
        if(StringUtils.isNotEmpty(addRes)){
            return DataResult.fail(addRes);
        }
        return DataResult.success("修改成功！");
    }

    @GetMapping("deleteFalse")
    @ApiOperation("逻辑删除套餐")
    public DataResult deleteFalse(Long id,String sureFlag){
        if(id == null){
            return DataResult.fail("主键不能未空");
        }
        String res = refMenuTempService.deleteById(id,sureFlag.equals("0")?false:true);
        if(StringUtils.isNotEmpty(res)){
            return DataResult.fail(res);
        }
        return DataResult.success("删除成功");
    }


    @GetMapping("tempMenu")
    @ApiOperation("获取到套餐菜单")
    public DataResult  tempMenu(String id){
        List<Menu>   menuList = refMenuTempService.listComboDetail(StringUtils.isEmpty(id)?null:Long.parseLong(id));
        List<TreeSelect>  menuTree = menuService.buildTreeSelect(menuList);

        Map<String,Object>  resMap = new HashMap<>();
        resMap.put("menuTree",menuTree);
        resMap.put("menuIdList",menuList.stream().map(Menu::getMenuId).collect(Collectors.toList()));
        return DataResult.success("查询成功！",resMap);
    }

}
